package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.ProductDAO;
import com.zerosky.crm.po.Product;
import com.zerosky.crm.service.ProductService;
import com.zerosky.crm.util.PageInfo;

@Service(value="productService")
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductDAO productDao;

	public List<Product> getProductList(Map map) {
		return productDao.getProductList(map);
	}

	/**
	 * 分页查询信息
	 */
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Product> getPageInfoList(Map map, PageInfo<Product> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();
		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<Product> list = productDao.getProductList(map);
		page.setList(list);
		int count = productDao.countProduct(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Integer saveOrUpdate(Product Product, String operation) {
		if (operation.equals("新增")) {
			return productDao.saveProduct(Product);
		} else if (operation.equals("修改")) {
			return productDao.updateProduct(Product);
		}
		return 0;

	}

	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Integer deleteProduct(Integer Product_id) {
		return productDao.deleteProductById(Product_id);

	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Product getProductById(Integer productId) {
		return productDao.getProductById(productId);
	}

}
